package com.wenhui.schoolyardinfo.firstpage.ui;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.callback.FlowLayoutHotSearchOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.FlowLayoutOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.SideIndexBar;
import com.wenhui.schoolyardinfo.firstpage.adapter.SchoolChoiceAdapter;
import com.wenhui.schoolyardinfo.firstpage.bean.SchoolChoiceBean;
import com.wenhui.schoolyardinfo.manager.IsEnglishLetterManager;
import com.wenhui.schoolyardinfo.manager.LogManager;
import com.wenhui.schoolyardinfo.manager.ScreenManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SchoolChoiceActivity extends BaseActivity implements RcvOnItemViewClickListener, FlowLayoutOnItemItemViewClickListener, FlowLayoutHotSearchOnItemItemViewClickListener {

    private static final String TAG = "SchoolChoiceActivity";
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.imv_search)
    ImageView imvSearch;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.tev_school_choice)
    TextView tevSchoolChoice;
    @BindView(R.id.tev_complete)
    TextView tevComplete;
    @BindView(R.id.layout_choice)
    RelativeLayout layoutChoice;
    @BindView(R.id.schools_recycler_view)
    RecyclerView schoolsRecyclerView;
    @BindView(R.id.cp_overlay)
    TextView cpOverlay;
    @BindView(R.id.cp_side_index_bar)
    SideIndexBar cpSideIndexBar;

    private SchoolChoiceAdapter schoolChoiceAdapter;
    private List<String> historicRecordList;
    private List<String> hotSchoolList;
    private List<SchoolChoiceBean> schoolChoiceBeanList;
    private LinearLayoutManager layoutManager;
    private int historicRecordsPosition;
    private int hotSchoolPosition;
    private boolean isRepeathistoricRecords;//历史记录是否重复了

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_choice);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {
        historicRecordList = new ArrayList<>();
        hotSchoolList = new ArrayList<>();
        historicRecordList.add("香港中文大学");
        historicRecordList.add("武汉理工大学");
        hotSchoolList.add("香港中文大学");
        hotSchoolList.add("澳门城市大学");
        hotSchoolList.add("北京大学");
        hotSchoolList.add("上海大学");
        hotSchoolList.add("杭州工业大学");
        hotSchoolList.add("上海交通大学");
        hotSchoolList.add("杭州大学");
        hotSchoolList.add("香港中文大学");
        hotSchoolList.add("香港中文大学");
        hotSchoolList.add("杭州大学");

        schoolChoiceBeanList = new ArrayList<>();
        schoolChoiceBeanList.add(new SchoolChoiceBean("A", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门科技大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门科技大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门城市大学", "a", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("澳门科技大学", "a", false));

        schoolChoiceBeanList.add(new SchoolChoiceBean("B", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京交通大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京工业大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京理工大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京科技大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京工商大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京建筑大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京外国语大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京交通大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京工业大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京理工大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京科技大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京工商大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京建筑大学", "b", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("北京外国语大学", "b", false));

        schoolChoiceBeanList.add(new SchoolChoiceBean("C", "c", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德医学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州师范学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德石油高等专科学校", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "chengde", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "chengde", false));


        schoolChoiceBeanList.add(new SchoolChoiceBean("D", "d", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德医学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州师范学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德石油高等专科学校", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德医学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州师范学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德石油高等专科学校", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "deyizhi", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "deyizhi", false));


        schoolChoiceBeanList.add(new SchoolChoiceBean("E", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德医学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州师范学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德石油高等专科学校", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春理工大学", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长春工业大学", "e", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("常州大学", "e", false));


        schoolChoiceBeanList.add(new SchoolChoiceBean("F", "f", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德医学院", "f", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州师范学院", "f", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("承德石油高等专科学校", "f", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("沧州职业技术学院", "f", false));
        schoolChoiceBeanList.add(new SchoolChoiceBean("长治医学院", "f", false));

        historicRecordsPosition = -1;
        hotSchoolPosition = -1;
        isRepeathistoricRecords = false;
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);
        topbar.removeAllLeftViews();

        cpSideIndexBar.setNavigationBarHeight(ScreenManager.getNavigationBarHeight(this));
        cpSideIndexBar.setOverlayTextView(cpOverlay)
                .setOnIndexChangedListener(new SideIndexBar.OnIndexTouchedChangedListener() {
                    @Override
                    public void onIndexChanged(String index, int position) {
                        //滚动RecyclerView到索引位置
                        schoolChoiceAdapter.scrollToSection(index);
                    }
                });
        initAdapter();
    }

    private void initAdapter() {
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);

        schoolsRecyclerView.setItemAnimator(new DefaultItemAnimator());
        schoolsRecyclerView.setLayoutManager(layoutManager);
        schoolChoiceAdapter = new SchoolChoiceAdapter(this);
        schoolChoiceAdapter.setLayoutManager(layoutManager);
        schoolsRecyclerView.setAdapter(schoolChoiceAdapter);
        schoolChoiceAdapter.setRcvOnItemViewClickListener(this);
        schoolChoiceAdapter.setFlowLayoutOnItemItemViewClickListener(this);
        schoolChoiceAdapter.setFlowLayoutHotSearchOnItemItemViewClickListener(this);

        schoolChoiceAdapter.clearData();
        schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
    }

    @Override
    public void onItemClickListener(int position, View view) {
        switch (view.getId()) {
            case R.id.tev_clean_up:
                schoolChoiceAdapter.clearData();
                historicRecordList.clear();
                schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
                break;
            case R.id.tev_exchange:
                hotSchoolList.clear();
                hotSchoolList.add("爱丁堡大学");
                hotSchoolList.add("加州分校");
                hotSchoolList.add("加州理工大学");
                hotSchoolList.add("多伦多大学");
                hotSchoolList.add("上海交通大学");
                hotSchoolList.add("伦敦大学学院");
                hotSchoolList.add("墨尔本大学");
                hotSchoolList.add("悉尼大学");
                hotSchoolList.add("慕尼黑大学");
                hotSchoolList.add("新加坡国立大学");
                schoolChoiceAdapter.clearData();
                schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
                break;
            case R.id.layout_school:
                if (!IsEnglishLetterManager.isEnglishLetter(schoolChoiceBeanList.get(position - 2).getName())) {
                    tevSchoolChoice.setText("选择学校:" + schoolChoiceBeanList.get(position - 2).getName());
                    for (int i = 0; i < schoolChoiceBeanList.size(); i++) {
                        if (i == position - 2) {
                            schoolChoiceBeanList.get(position - 2).setChecked(true);
                        } else {
                            schoolChoiceBeanList.get(i).setChecked(false);
                        }
                    }

                    historicRecordsPosition = -1;
                    hotSchoolPosition = -1;


                    for (int i = 0; i < historicRecordList.size(); i++) {
                        if (schoolChoiceBeanList.get(position - 2).getName().equals(historicRecordList.get(i))) {//判断历史记录中是否有这个字段
                            isRepeathistoricRecords = true;
                        }
                    }
                    if (!isRepeathistoricRecords) {//如果没有，就添加
                        historicRecordList.add(0, schoolChoiceBeanList.get(position - 2).getName());
                    }
                    if (historicRecordList.size() > 10) {
                        historicRecordList.remove(10);
                    }
                    isRepeathistoricRecords = false;

                    schoolChoiceAdapter.clearData();
                    schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
                }
                break;
//            case R.id.tev_clean_up:
//
//                break;
//            case R.id.tev_clean_up:
//
//                break;
        }
    }

    @Override
    public void onItemItemFlowLayoutClickListener(int position, View view) {
        showToast("点击了子项");
        historicRecordsPosition = position;
        hotSchoolPosition = -1;
        tevSchoolChoice.setText("选择学校:" + historicRecordList.get(position));
        for (int i = 0; i < schoolChoiceBeanList.size(); i++) {
            schoolChoiceBeanList.get(i).setChecked(false);
        }
        schoolChoiceAdapter.clearData();
        schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
    }

    @Override
    public void onItemItemFlowLayoutHotSearchClickListener(int position, View view) {
        showToast("点击了子项");
        historicRecordsPosition = -1;
        hotSchoolPosition = position;
        tevSchoolChoice.setText("选择学校:" + hotSchoolList.get(position));
        for (int i = 0; i < schoolChoiceBeanList.size(); i++) {
            schoolChoiceBeanList.get(i).setChecked(false);
        }

        for (int i = 0; i < historicRecordList.size(); i++) {
            if (hotSchoolList.get(position).equals(historicRecordList.get(i))) {//判断历史记录中是否有这个字段
                isRepeathistoricRecords = true;
            }
        }
        if (!isRepeathistoricRecords) {//如果没有，就添加
            historicRecordList.add(0, hotSchoolList.get(position));
        }
        if (historicRecordList.size() > 10) {
            historicRecordList.remove(10);
        }
        isRepeathistoricRecords = false;

        schoolChoiceAdapter.clearData();
        schoolChoiceAdapter.addAllData(historicRecordList, hotSchoolList, historicRecordsPosition, hotSchoolPosition, schoolChoiceBeanList);
    }

    @OnClick({R.id.layout_back, R.id.imv_search, R.id.layout_choice, R.id.cp_side_index_bar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.imv_search:
                startActivity(SearchActivity.class);
                break;
            case R.id.layout_choice:
                break;
            case R.id.cp_side_index_bar:
                break;
        }
    }

}
